#include "gameselect.h"
#include "gameroom.h"
#include "widget.h"
#include <QIcon>
#include <QPainter>
#include <QIcon>
#include <QPushButton>
#include <QFont>
#include <QSound>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    //首先设置窗口图标，和窗口的标题
    //设置窗口的图标
    this->setWindowIcon(QIcon(":res/icon.png"));
    //设置窗口的标题
    this->setWindowTitle("贪吃蛇关卡选择");

    //创建返回按钮,设置按钮的位置和图标以及行为
    QPushButton * backBtn = new QPushButton(this);
    backBtn->move(900,600);
    backBtn->setIcon(QIcon(":/res/backicon.png"));
    backBtn->resize(40,40);
    QString style("");
    style+="QPushButton {border:0px;}";
    style+="QPushButton:hover { background-color: rgb(255, 223, 0); }";
    backBtn->setStyleSheet(style);
    connect(backBtn,&QPushButton::clicked,[=](){
        //关闭当前窗口
        this->close();
        Widget * greedly_hall = new Widget;
        greedly_hall->show();
        QSound::play(":res/clicked.wav");
    });

    //创建关卡选择按钮，内容及其事件以及样式
    QString style1("");
    style1+=        "QPushButton:pressed {"
                    "    background-color: #2e86c1;"
                    "    border: 2px solid #1c5d99;"
                    "    color: #eaf2f8;"
                    "}";
    style1+="QPushButton:hover{background-color: #5dade2; border: 2px solid #1f618d;}";
    style1+="QPushButton {color: white;background-color: #3498db;border: 2px solid #2980b9;border-radius: 20px; /* 椭圆形边框 */padding: 10px 20px;font-size: 20px;}";
    QFont font("华文行楷",15);
    QPushButton * simpleBtn = new QPushButton(this);
    simpleBtn->setText("简单模式");
    simpleBtn->move(430,500);
    simpleBtn->setFont(font);
    simpleBtn->setStyleSheet(style1);
    //为按钮设置事件
    handle(simpleBtn);

    QPushButton * normalBtn = new QPushButton(this);
    normalBtn->setText("正常模式");
    normalBtn->move(430,550);
    normalBtn->setFont(font);
    normalBtn->setStyleSheet(style1);
    //为按钮设置事件
    handle(normalBtn);

    QPushButton * difficultyBtn = new QPushButton(this);
    difficultyBtn->setText("困难模式");
    difficultyBtn->move(430,600);
    difficultyBtn->setFont(font);
    difficultyBtn->setStyleSheet(style1);
    //为按钮设置事件
    handle(difficultyBtn);


    QPushButton * historyBtn = new QPushButton(this);
    historyBtn->setText("历史战绩");
    historyBtn->move(430,650);
    historyBtn->setFont(font);
    historyBtn->setStyleSheet(style1);
}

void GameSelect::paintEvent(QPaintEvent *event)
{
    (void)event;
    //实例话一个绘画者
    QPainter painter(this);

    //创建绘画工具
    QPixmap map(":res/gameSelect.png");

    //使用绘画工具将背景绘画到窗口上
    painter.drawPixmap(0,0,this->width(),this->height(),map);
}

void GameSelect::handle(QPushButton *p1)
{
    connect(p1,&QPushButton::clicked,[=](){
        this->close();
        GameRoom * gameroom = new GameRoom;
        gameroom->setGeometry(this->geometry());
        gameroom->show();
        QSound::play(":res/clicked.wav");
    });
}
